﻿
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Apps.Extensions.Mapping
{
    public static class AutoMapperExtensions
    {

        public static IServiceCollection AutoMapper(this IServiceCollection services)
        {

            services.AddAutoMapper(typeof(AutoMapperConfig));
            return services;
        }
 

        // 传递 IMapper 实例
        public static TDestination To<TDestination>(this object source, IMapper mapper)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return mapper.Map<TDestination>(source);
        }

        // 传递 IMapper 实例
        public static IEnumerable<TDestination> To<TDestination>(this IEnumerable<object> source, IMapper mapper)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return source.Select(x => mapper.Map<TDestination>(x)).ToList();
        }


    }
}
